package com.magina.mq.rabbit.queue;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class RabQueueWithComfirmSender implements RabbitTemplate.ConfirmCallback  {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${rab.queueName}")
    private String rabQueueName;

    public void send(Integer i){
        String msg = "rabbitMq send queue msg: count :" + i;
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        System.out.println("callbackSender UUID: " + correlationData.getId());
        rabbitTemplate.setConfirmCallback(this);
//        rabbitTemplate.setReturnCallback(this);
        this.rabbitTemplate.convertAndSend(rabQueueName, (Object) msg, correlationData);
    }

    @Autowired
    public RabQueueWithComfirmSender(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
       this.rabbitTemplate.setConfirmCallback(this);
    }
    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        System.out.println("confirm: " + correlationData.getId() + ",s=" + s + ",b:" + b);
    }

//    @Override
//    public void returnedMessage(Message message, int i, String s, String s1, String s2) {
//        System.out.println("sender return success" + message.toString()+"==="+i+"==="+s1+"==="+s2);
//    }
}
